package com.hitsoysauce.date20121229.launcher;

import java.util.Arrays;

public class TestArray {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//about arraycopy
		int[] copyFrom = {10,11,12,13,14,15};
		
		int[] copyTo = new int[6];
		
		System.arraycopy(copyFrom, 0, copyTo, 0, copyFrom.length);
		
		
		for (int item : copyTo) {
			System.out.print(item + " ");
		}
		System.out.println();
		
		
		int[] smallPrimes = {2,3,5,7,11,13};
		int[] luckyNumbers =  {1001,1002,1003,1004,1005,1006,1007};
		
		System.arraycopy(smallPrimes, 2, luckyNumbers, 3, 4);
		
		for (int item : luckyNumbers) {
			System.out.print(item + " ");
		}
		System.out.println();
		
		//array sort
		Arrays.sort(luckyNumbers);
		for (int item : luckyNumbers) {
			System.out.print(item + " ");
		}
		System.out.println();
		
		//binarySearch
		int index = Arrays.binarySearch(luckyNumbers, 11);
		index = binarySearch0(luckyNumbers, 0, luckyNumbers.length, 11);
		System.out.println("luckyNumbers[" + index + "] = " + luckyNumbers[index]);
	}
	
	 // Like public version, but without range checks.
    private static int binarySearch0(int[] a, int fromIndex, int length, int key) {
		int low = fromIndex;
		int high = length - 1;
	
		while (low <= high) {
		    int mid = (low + high) >>> 1;
		    int midVal = a[mid];
	
		    if (midVal < key)
			low = mid + 1;
		    else if (midVal > key)
			high = mid - 1;
		    else
			return mid; // key found
		}
		return -(low + 1);  // key not found.
    }
	
}
